home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / dos2unix.zip / UNIX2DOS.C < prev    next >
C/C++ Source or Header  |  1989-07-04  |  2KB  |  116 lines

  1. /*
  2.  *                              UNIX2DOS.C
  3.  *
  4.  * Convert lf's to crlf combinations in a file but keep it's original
  5.  * date/time stamp.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #ifndef TRUE
  12. #    define TRUE  (1)
  13. #    define FALSE (0)
  14. #endif
  15.  
  16. #if MSDOS
  17. #    define link(x,y) rename (x, y)
  18. #    define R_CNTRL   "rb"
  19. #    define W_CNTRL   "wb"
  20. #else
  21. #    define R_CNTRL   "r"
  22. #    define W_CNTRL   "w"
  23. #endif
  24.  
  25.  
  26. struct stat s_buf;
  27.  
  28.  
  29.  
  30.  
  31. main (argc, argv)
  32. int argc;
  33. char **argv;
  34. {
  35.     char *path;
  36.     while (--argc>0)
  37.     {
  38.         if (stat (path=*++argv, &s_buf) != -1)
  39.         {
  40.             printf ("Unix2Dos: Cleaning file %s ...\n", path);
  41.             if (u2dos (path))
  42.             {
  43.                 fprintf (stderr, "Unix2Dos: Problems cleaning file %s\n", path);
  44.             }
  45.         }
  46.         else
  47.         {
  48.             fprintf (stderr, "Unix2Dos: Can't stat '%s'\n", path);
  49.         }
  50.     }
  51. }
  52.  
  53.  
  54.  
  55.  
  56. int
  57. u2dos (path)
  58. char *path;
  59. {
  60.     FILE *in, *out;
  61.     int ch,
  62.         rval = FALSE;
  63.     char temppath [16];
  64.     struct utimbuf { time_t actime, modtime; } ut_buf;
  65.  
  66.     strcpy (temppath, "./clntmp");
  67. #if !defined (MSDOS)
  68.     strcat (temppath, "XXXXXX");
  69.     mktemp (temppath);
  70. #endif    
  71.     if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
  72.         return TRUE;
  73.     if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
  74.     {
  75.         fclose (in);
  76.         return TRUE;
  77.     }
  78.     while ((ch = getc (in)) != EOF)
  79.         if (((ch == '\012') && (putc ('\015', out) == EOF)) ||
  80.             (putc (ch, out) == EOF)                   )
  81.         {
  82.             rval = TRUE;
  83.             break;
  84.         }
  85.     if (fclose (in) == EOF)
  86.     {
  87.         rval = TRUE;
  88.     }
  89.     if (fclose (out) == EOF)
  90.     {
  91.         rval = TRUE;
  92.     }
  93.     ut_buf.actime = s_buf.st_atime;
  94.     ut_buf.modtime = s_buf.st_mtime;
  95.     if (utime (temppath, &ut_buf) == -1)
  96.         rval = TRUE;
  97.     if (unlink (path) == -1)
  98.         rval = TRUE;
  99.     if (rval)
  100.     {
  101.         unlink (temppath);
  102.         return TRUE;
  103.     }
  104.     if (link (temppath,path) == -1)
  105.     {
  106.         fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%s'\n", temppath, path);
  107.         fprintf (stderr, "          However, file '%s' remains\n", temppath);
  108.         exit (1);
  109.     }
  110.     unlink (temppath);
  111.     return FALSE;
  112. }
  113.     
  114.         
  115.     
  116.